namespace Vin.Tool.Core.IEnumerableCategory;

public static partial class Extension
{
    public static string JoinToString<T>(this IEnumerable<T>? source, string separator = ",")
    {
        var tempSource = source?.ToList();
        if (tempSource == null || tempSource.Count <= 0) return string.Empty;
        if (string.IsNullOrEmpty(separator)) return string.Join("", tempSource!);
        return string.Join(separator, tempSource!);
    }

    public static IEnumerable<T>? Shuffle<T>(this IEnumerable<T>? source)
    {
        var random = new Random();
        return source?.OrderBy(x => random.Next());
    }

    public static T? RandomElement<T>(this IEnumerable<T>? source)
    {
        if (source == null) return default;
        return source.Shuffle()!.FirstOrDefault();
    }
}